<html>
<head><meta charset="utf-8"><title>How do I get the type of an `ast::ItemKind::Fn`? · t-compiler/help · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/index.html">t-compiler/help</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/How.20do.20I.20get.20the.20type.20of.20an.20.60ast.3A.3AItemKind.3A.3AFn.60.3F.html">How do I get the type of an `ast::ItemKind::Fn`?</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="201454270"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/How%20do%20I%20get%20the%20type%20of%20an%20%60ast%3A%3AItemKind%3A%3AFn%60%3F/near/201454270" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Joshua Nelson <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/How.20do.20I.20get.20the.20type.20of.20an.20.60ast.3A.3AItemKind.3A.3AFn.60.3F.html#201454270">(Jun 20 2020 at 00:48)</a>:</h4>
<p>Is this happening too early in the process to infer the type or is there a way to get it from <code>tcx</code>?</p>



<a name="201454298"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/How%20do%20I%20get%20the%20type%20of%20an%20%60ast%3A%3AItemKind%3A%3AFn%60%3F/near/201454298" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Joshua Nelson <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/How.20do.20I.20get.20the.20type.20of.20an.20.60ast.3A.3AItemKind.3A.3AFn.60.3F.html#201454298">(Jun 20 2020 at 00:49)</a>:</h4>
<p>(deleted)</p>



<a name="201454304"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/How%20do%20I%20get%20the%20type%20of%20an%20%60ast%3A%3AItemKind%3A%3AFn%60%3F/near/201454304" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Joshua Nelson <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/How.20do.20I.20get.20the.20type.20of.20an.20.60ast.3A.3AItemKind.3A.3AFn.60.3F.html#201454304">(Jun 20 2020 at 00:49)</a>:</h4>
<p>actually I think I see the issue already, I don't have a <code>tcx</code> available :(</p>



<a name="201454360"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/How%20do%20I%20get%20the%20type%20of%20an%20%60ast%3A%3AItemKind%3A%3AFn%60%3F/near/201454360" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Joshua Nelson <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/How.20do.20I.20get.20the.20type.20of.20an.20.60ast.3A.3AItemKind.3A.3AFn.60.3F.html#201454360">(Jun 20 2020 at 00:50)</a>:</h4>
<p>for context (hah!) this is in <a href="https://github.com/rust-lang/rust/blob/2bfdecad6b1e89c4becfd4c115f643384eb39d6a/src/librustc_interface/util.rs#L594">https://github.com/rust-lang/rust/blob/2bfdecad6b1e89c4becfd4c115f643384eb39d6a/src/librustc_interface/util.rs#L594</a> which is implementing <code>MutVisitor</code> for the AST</p>



<a name="201456301"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/How%20do%20I%20get%20the%20type%20of%20an%20%60ast%3A%3AItemKind%3A%3AFn%60%3F/near/201456301" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> mark-i-m <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/How.20do.20I.20get.20the.20type.20of.20an.20.60ast.3A.3AItemKind.3A.3AFn.60.3F.html#201456301">(Jun 20 2020 at 01:43)</a>:</h4>
<p>Yeah, so the AST happens before any typing has happened</p>



<a name="201456356"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/How%20do%20I%20get%20the%20type%20of%20an%20%60ast%3A%3AItemKind%3A%3AFn%60%3F/near/201456356" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> mark-i-m <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/How.20do.20I.20get.20the.20type.20of.20an.20.60ast.3A.3AItemKind.3A.3AFn.60.3F.html#201456356">(Jun 20 2020 at 01:44)</a>:</h4>
<p>However, depending on where in <code>rustc_interface</code> this is, you might be able to get access to a <code>tcx</code> somehow...</p>



<a name="201456449"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/How%20do%20I%20get%20the%20type%20of%20an%20%60ast%3A%3AItemKind%3A%3AFn%60%3F/near/201456449" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> mark-i-m <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/How.20do.20I.20get.20the.20type.20of.20an.20.60ast.3A.3AItemKind.3A.3AFn.60.3F.html#201456449">(Jun 20 2020 at 01:47)</a>:</h4>
<p>hmmm... yeah, looking through the code, it looks like this is before the <code>tcx</code> is even constructed</p>



<a name="201456488"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/How%20do%20I%20get%20the%20type%20of%20an%20%60ast%3A%3AItemKind%3A%3AFn%60%3F/near/201456488" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> mark-i-m <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/How.20do.20I.20get.20the.20type.20of.20an.20.60ast.3A.3AItemKind.3A.3AFn.60.3F.html#201456488">(Jun 20 2020 at 01:48)</a>:</h4>
<p>(though, I'm not an expert, so I may be wrong)</p>



<a name="201456763"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/How%20do%20I%20get%20the%20type%20of%20an%20%60ast%3A%3AItemKind%3A%3AFn%60%3F/near/201456763" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Joshua Nelson <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/How.20do.20I.20get.20the.20type.20of.20an.20.60ast.3A.3AItemKind.3A.3AFn.60.3F.html#201456763">(Jun 20 2020 at 01:57)</a>:</h4>
<p>ok, well it's not necessary to fix my bug, it would just be nice (allow replacing bodies of functions returning <code>impl trait</code>). thanks for looking though!</p>



<a name="201459362"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/How%20do%20I%20get%20the%20type%20of%20an%20%60ast%3A%3AItemKind%3A%3AFn%60%3F/near/201459362" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> mark-i-m <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/How.20do.20I.20get.20the.20type.20of.20an.20.60ast.3A.3AItemKind.3A.3AFn.60.3F.html#201459362">(Jun 20 2020 at 03:25)</a>:</h4>
<p>I might be misunderstanding, but why not use a proc macro?</p>



<a name="201459637"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/How%20do%20I%20get%20the%20type%20of%20an%20%60ast%3A%3AItemKind%3A%3AFn%60%3F/near/201459637" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Joshua Nelson <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/How.20do.20I.20get.20the.20type.20of.20an.20.60ast.3A.3AItemKind.3A.3AFn.60.3F.html#201459637">(Jun 20 2020 at 03:35)</a>:</h4>
<p>I'm not sure how proc macros are related. In this context I'm replacing the function body with <code>loop {}</code> so that rustdoc doesn't have to do item-bodies checking, but that breaks type inference if the function returns <code>impl trait</code>.</p>



<a name="201490525"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/How%20do%20I%20get%20the%20type%20of%20an%20%60ast%3A%3AItemKind%3A%3AFn%60%3F/near/201490525" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> mark-i-m <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/How.20do.20I.20get.20the.20type.20of.20an.20.60ast.3A.3AItemKind.3A.3AFn.60.3F.html#201490525">(Jun 20 2020 at 18:26)</a>:</h4>
<p>Ah, I see. Yeah, ignore my suggestion :P</p>



<a name="201504198"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/How%20do%20I%20get%20the%20type%20of%20an%20%60ast%3A%3AItemKind%3A%3AFn%60%3F/near/201504198" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Joshua Nelson <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/How.20do.20I.20get.20the.20type.20of.20an.20.60ast.3A.3AItemKind.3A.3AFn.60.3F.html#201504198">(Jun 20 2020 at 23:36)</a>:</h4>
<p>In case you're curious, this ended up as <a href="https://github.com/rust-lang/rust/pull/73566/">https://github.com/rust-lang/rust/pull/73566/</a></p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>